
//
//  PersonViewController.m
//  shuilijuOASystem
//
//  Created by michael on 16/7/12.
//  Copyright © 2016年 czd. All rights reserved.
//
#import "OADepAddressController.h"
#import "PersonViewController.h"
#import "OAAddressDetailViewController.h"
@interface PersonViewController ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation PersonViewController

-(NSMutableArray*)contentsArray{

    if (_contentsArray == nil) {
        _contentsArray = [NSMutableArray array];
    }
    
    return _contentsArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"单位部门选择";
    _tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
    __weak typeof(self) weakSelf = self;
   
    self.currentPage = 1;
    [self requestData];

        [self setRightItem];

    
        [self.tableView addFooterWithCallback:^{
            if ((self.currentPage - 1)*KPageSize < self.totalNumber) {
            
            
            if (weakSelf.isNeedRefresh) {
    
}else{
                 [weakSelf requestMoreData];
                 }
            }else{
                [weakSelf.view.window makeToast:@"没有更多数据了"];
                [weakSelf.tableView footerEndRefreshing];
            }
           
        }];
//    }
}
-(void)setRightItem{
    UIBarButtonItem * rightitem = [[UIBarButtonItem alloc] initWithTitle:@"搜索" style:UIBarButtonItemStylePlain target:self action:@selector(searchAction)];
    self.navigationItem.rightBarButtonItem = rightitem;
}

-(void)searchAction{

    UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"搜索通讯录" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    alertView.delegate = self;
    alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
    UITextField * textField = [alertView textFieldAtIndex:0];
    textField.placeholder = @"请输入关键字";
    [alertView show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    if (buttonIndex == 1) {
        UITextField * textField = [alertView textFieldAtIndex:0];
        if (textField.text.length == 0) {
            return;
        }
        OADepAddressController *per = [[OADepAddressController alloc]init];
    per.isNeedRefresh = YES;
    per.string = textField.text;
    [self.navigationController pushViewController:per animated:YES];
    
}
    
}


//请求数据
-(void)requestData{
      NSArray *  array = @[@{@"path":self.unid}];

 [HttpTools requestWithURL:KExchother arrays:array method:@"BmTxlTree" success:^(id JSON) {
            [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];

        [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
        NSArray * tempArray = JSON[@"data"];
        NSDictionary *dict = [tempArray firstObject];
        self.contents = dict[@"Parent"];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
         [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
    }];

    
}
//加载更多
-(void)requestMoreData{
 NSArray *  array = @[@{@"path":self.unid}];

 [HttpTools requestWithURL:@"exchother" arrays:array method:@"BmTxlTree" success:^(id JSON) {
            [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];

        [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
        NSArray * tempArray = JSON[@"data"];
        NSDictionary *dict = [tempArray firstObject];
        self.contents = dict[@"Parent"];
        [self.tableView reloadData];
                self.currentPage++;

    } failure:^(NSError *error) {
         [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
    }];

    
    
}

-(void)refreshData{
    self.currentPage = 1;
    [self requestData];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

     static NSString * cellIdentifier = @"cellId";
            UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
            }
    NSDictionary *dict = self.contents[indexPath.row];
//    cell.title.text = dict[@"surname"];
    cell.textLabel.text = dict[@"name"];
//    cell.des.text = dict[@"duties"];
//    cell.des.numberOfLines = 2;
//    cell.title.layer.cornerRadius = 10;
//    cell.title.layer.masksToBounds = YES;
//    cell.title.backgroundColor = [self randomColor];
    return cell;
}

-(UIColor *) randomColor
{
    CGFloat hue = ( arc4random() % 256 / 256.0 ); //0.0 to 1.0
    CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0,away from white
    CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5; //0.5 to 1.0,away from black
    return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.contents.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
     NSDictionary * dict = self.contents[indexPath.row];

    UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    OADepAddressController * depVC = (OADepAddressController*)[storyBoard instantiateViewControllerWithIdentifier:@"DepAddressID" ];
    depVC.path = dict[@"unid"];
    [self.navigationController pushViewController:depVC animated:YES];

}

@end
